<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="100px">
      <el-form-item label="设备名称" prop="equipmentName">
        <el-input v-model="queryParams.equipmentName" placeholder="请输入监控设备名称" clearable
          @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="设备编码" prop="equipmentCode">
        <el-input v-model="queryParams.equipmentCode" placeholder="请输入监控设备编码" clearable
          @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="自动巡检" prop="useInspection">
        <el-select v-model="queryParams.useInspection" placeholder="启用自动巡检" :onSelectionChange="handleQuery" clearable>
          <el-option v-for="dict in dict.type.sys_normal_disable" :key="dict.value" :label="dict.label"
            :value="dict.value" />
        </el-select>
      </el-form-item>
      <el-form-item label="是否启用" prop="status">
        <el-select v-model="queryParams.useWarning" placeholder="启用设备" :onSelectionChange="handleQuery" clearable>
          <el-option v-for="dict in dict.type.sys_normal_disable" :key="dict.value" :label="dict.label"
            :value="dict.value" />
        </el-select>
      </el-form-item>
      <el-form-item label="开启异常接收" prop="status">
        <el-select v-model="queryParams.useWarning" placeholder="开启异常接收" :onSelectionChange="handleQuery" clearable>
          <el-option v-for="dict in dict.type.sys_normal_disable" :key="dict.value" :label="dict.label"
            :value="dict.value" />
        </el-select>
      </el-form-item>
      <el-form-item label="开启异常推送" prop="useWarningPush">
        <el-select v-model="queryParams.useWarningPush" placeholder="开启数字孪生异常推送" :onSelectionChange="handleQuery"
          clearable>
          <el-option v-for="dict in dict.type.sys_normal_disable" :key="dict.value" :label="dict.label"
            :value="dict.value" />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd"
          v-hasPermi="['platform:equipment:add']">新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="success" plain icon="el-icon-edit" size="mini" :disabled="single" @click="handleUpdate"
          v-hasPermi="['platform:equipment:edit']">修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="danger" plain icon="el-icon-delete" size="mini" :disabled="multiple" @click="handleDelete"
          v-hasPermi="['platform:equipment:remove']">删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="warning" plain icon="el-icon-download" size="mini" @click="handleExport"
          v-hasPermi="['platform:equipment:export']">导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="equipmentList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="系统编号" align="center" prop="equipmentId" />
      <el-table-column label="仓库名称" align="center" prop="storeId" />
      <el-table-column label="设备名称" width="80" align="center" prop="equipmentName" />
      <el-table-column label="设备编码" width="80" align="center" prop="equipmentCode" />
      <el-table-column label="监控设备IP" width="100" align="center" prop="equipmentIp" />
      <el-table-column label="监控设备用户名" width="120" align="center" prop="equipmentUserName" />
      <el-table-column label="监控设备密码" width="100" align="center" prop="equipmentPassword" />
      <el-table-column label="设备通道" width="80" align="center" prop="equipmentChannel" />
      <el-table-column label="设备类型" prop="equipmentType" width="120">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.platform_equipment_type" :value="scope.row.equipmentType" />
        </template>
      </el-table-column>
      <el-table-column label="设备运行状态" width="100" align="center" prop="equipmentStatus" />
      <!-- <el-table-column label="备注" align="center" prop="memo" /> -->
      <el-table-column label="是否启用" align="center" key="useStatus" prop="useStatus" fixed="right">
        <template slot-scope="scope">
          <el-switch v-model="scope.row.useStatus" :active-value="1" :inactive-value="0"
            @change="handleUse(scope.row)"></el-switch>
        </template>
      </el-table-column>
      <el-table-column label="自动巡检" width="80" align="center" prop="useInspection" fixed="right">
        <template slot-scope="scope">
          <el-switch v-model="scope.row.useInspection" :active-value="1" :inactive-value="0"
            @change="handleUse(scope.row)"></el-switch>
        </template>
      </el-table-column>
      <el-table-column label="异常接收" width="80" align="center" prop="useWarningPush" fixed="right">
        <template slot-scope="scope">
          <el-switch v-model="scope.row.useWarning" :active-value="1" :inactive-value="0"
            @change="handleUse(scope.row)"></el-switch>
        </template>
      </el-table-column>
      <el-table-column label="操作" width="80" align="center" class-name="small-padding fixed-width" fixed="right">
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)"
            v-hasPermi="['platform:equipment:edit']">修改</el-button>
          <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)"
            v-hasPermi="['platform:equipment:remove']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
      @pagination="getList" />

    <!-- 添加或修改【请填写功能名称】对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="800px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="120px">
        <el-row>
          <el-col :span="12">
            <el-form-item label="选择仓库：" prop="storeId" v-loading="initData.loading">
              <el-select v-model="form.storeId" :value="form.storeName" placeholder="请选择仓库" style="width: 240px" >
                <el-option v-for="item in initData.options" :key="item.storeId" :label="item.storeName"
                  :value="item.storeId" />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item type="string" label="设备名称：" prop="equipmentName">
              <el-input v-model="form.equipmentName" placeholder="请输入监控设备名称" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="设备编码：" prop="equipmentCode">
              <el-input v-model="form.equipmentCode" placeholder="请输入监控设备编码" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="设备IP：" prop="equipmentIp">
              <el-input v-model="form.equipmentIp" placeholder="请输入监控设备IP" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="设备用户名：" prop="equipmentUserName">
              <el-input v-model="form.equipmentUserName" placeholder="请输入监控设备用户名" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="设备密码：" prop="equipmentPassword">
              <el-input v-model="form.equipmentPassword" placeholder="请输入监控设备密码" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="设备通道：" prop="equipmentChannel">
              <el-input v-model="form.equipmentChannel" placeholder="请输入设备通道" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="类型：" prop="equipmentType">
              <el-select v-model="form.equipmentType" placeholder="设备类型" clearable>
                <el-option v-for="dict in dict.type.platform_equipment_type" :key="dict.value" :label="dict.label"
                  :value="dict.value" />
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="开启异常接收：" prop="useWarning">
              <el-switch v-model="form.useWarning" :active-value="1" :inactive-value="0" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="开启异常推送：" prop="useWarningPush">
              <el-switch v-model="form.useWarningPush" :active-value="1" :inactive-value="0" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="开启自动巡检：" prop="useInspection">
              <el-switch v-model="form.useInspection" :active-value="1" :inactive-value="0" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="启用状态：" prop="useStatus">
              <el-switch v-model="form.useStatus" :active-value="1" :inactive-value="0" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-form-item label="备注：" prop="memo">
          <el-input v-model="form.memo" type="textarea" placeholder="请输入备注" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listEquipment, getEquipment, delEquipment, addEquipment, updateEquipment } from "@/api/platform/equipment"
import { listStore } from "@/api/platform/store"
import { reactive, ref } from 'vue'



export default {
  name: "Equipment",
  dicts: ['sys_normal_disable', 'platform_equipment_type'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 【请填写功能名称】表格数据
      equipmentList: [],
      // 弹出层标题
      title: "监控设备",
      // 是否显示弹出层
      open: false,
      //弹框下拉选择框数据
      initData: {
        loading: true,
        options: []
      },
      isEdit: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        storeId: null,
        equipmentName: null,
        equipmentCode: null,
        equipmentIp: null,
        equipmentUserName: null,
        equipmentPassword: null,
        equipmentChannel: null,
        equipmentType: null,
        equipmentStatus: null,
        memo: null,
        useStatus: null,
        useInspection: null,
        useWarning: null,
        useWarningPush: null,
      },
      // 表单参数
      form: {
        useStatus: 1,
        useInspection: 0,
        useWarning: 0,
        useWarningPush: 0,
      },
      // 表单校验
      rules: {
        storeId: [
          { required: true, message: "请选择仓库", trigger: 'change' }
        ],
        equipmentName: [
          { required: true, message: "监控设备名称不能为空", trigger: "blur" },
          { min: 1, max: 25, message: "监控设备名称长度不能超过25个汉字", trigger: "blur" }
        ],
        equipmentCode: [
          { required: true, message: "监控设备编码不能为空", trigger: "blur" },
          { min: 1, max: 36, message: "监控设备名称长度不能超过36个字母", trigger: "blur" }
        ],
        equipmentIp: [
          { required: true, message: "监控设备IP不能为空", trigger: "blur" },
          { min: 1, max: 36, message: "监控设备IP长度不能超过36个字母", trigger: "blur" }
        ],
        equipmentUserName: [
          { required: true, message: "监控设备用户名不能为空", trigger: "blur" },
          { min: 1, max: 36, message: "监控设备用户名长度不能超过36个字母", trigger: "blur" }
        ],
        equipmentPassword: [
          { required: true, message: "监控设备密码不能为空", trigger: "blur" },
          { min: 1, max: 36, message: "监控设备密码长度不能超过36个字母", trigger: "blur" }
        ],
        equipmentChannel: [
          { required: true, message: "设备通道不能为空", trigger: "blur" }
        ],
        useStatus: [
          { required: true, message: "是否启用不能为空", trigger: "change" }
        ],
        useInspection: [
          { required: true, message: "开启自动巡检不能为空", trigger: "change" }
        ],
        useWarning: [
          { required: true, message: "开启异常接收不能为空", trigger: "change" }
        ],
        useWarningPush: [
          { required: true, message: "开启数字孪生异常推送不能为空", trigger: "blur" }
        ],
        equipmentType: [
          { required: true, message: "请选择设备类型", trigger: "change" }
        ],
      }
    }
  },
  created() {
    this.getList()
    this.loadStore();
  },
  methods: {
    /** 查询【请填写功能名称】列表 */
    getList() {
      this.loading = true
      listEquipment(this.queryParams).then(response => {
        this.equipmentList = response.rows
        this.total = response.total
        this.loading = false
      })
    },
    // 取消按钮
    cancel() {
      this.open = false
      this.reset()
    },
    // 表单重置
    reset() {
      this.form = {
        equipmentId: null,
        storeId: null,
        equipmentName: null,
        equipmentCode: null,
        equipmentIp: null,
        equipmentUserName: null,
        equipmentPassword: null,
        equipmentChannel: null,
        equipmentType: null,
        equipmentStatus: null,
        memo: null,
        useStatus: 1,
        useInspection: 0,
        useWarning: 0,
        useWarningPush: 0,
        delFlag: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null
      }
      this.resetForm("form")
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm")
      this.handleQuery()
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.equipmentId)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset()
      this.form.useStatus = 1
      this.open = true
      this.title = "添加监控设备"
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.isEdit = true
      this.initData.loading = false
      this.reset()
      const equipmentId = row.equipmentId || this.ids
      getEquipment(equipmentId).then(response => {
        this.form = response.data
        this.open = true
        this.title = "修改监控设备"
      })
    },
    /** 提交按钮 */
    submitForm() {
      console.log(this.form)
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.equipmentId != null) {
            updateEquipment(this.form).then(response => {
              this.$modal.msgSuccess("修改成功")
              this.open = false
              this.getList()
            })
          } else {
            addEquipment(this.form).then(response => {
              this.$modal.msgSuccess("新增成功")
              this.open = false
              this.getList()
            })
          }
        }
      })
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const equipmentIds = row.equipmentId || this.ids
      this.$modal.confirm('是否确认删除【请填写功能名称】编号为"' + equipmentIds + '"的数据项？').then(function () {
        return delEquipment(equipmentIds)
      }).then(() => {
        this.getList()
        this.$modal.msgSuccess("删除成功")
      }).catch(() => { })
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('platform/equipment/export', {
        ...this.queryParams
      }, `equipment_${new Date().getTime()}.xlsx`)
    },
    handleUse(row) {
      this.$modal.confirm('是否确认当前执行操作').then(function () {
        return updateEquipment(row)
      }).then(() => {
        // this.getList()
        this.$modal.msgSuccess("操作成功")
      }).catch(() => { })
    },
    /** 加载仓库下拉**/
    loadStore() {
      this.initData.loading = true
      listStore({}).then(response => {
        this.initData.options = response.rows
        this.initData.loading = false
      })
    }
  }
}
</script>
